package com.walk.zworld.ui.activity.my

import android.content.Intent
import android.os.Build
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.widget.SwitchCompat
import com.jeremyliao.liveeventbus.LiveEventBus
import com.walk.zworld.view.RvAnimDialog
import com.walk.zworld.R
import com.walk.zworld.mvvm.Constant
import com.walk.zworld.utils.CacheDataUtil
import com.walk.zworld.view.MyDialog
import com.walk.zworld.view.TopDialog
import com.base.lib.base.utils.MyMMKV.Companion.mmkv
import com.base.lib.base.utils.SettingUtil
import com.base.lib.base.utils.StatusBarUtils
import com.base.lib.base.view.BaseActivity

class SystemActivity : BaseActivity() {

    private var sc_system_is_img: SwitchCompat? = null
    private var sc_system_show_top: SwitchCompat? = null
    private var sc_system_show_banner: SwitchCompat? = null
    private var sc_system_show_badge: SwitchCompat? = null
    private var rl_app_default_page: RelativeLayout? = null
    private var rl_rv_anmi: RelativeLayout? = null
    private var rl_system_clear: RelativeLayout? = null
    private var rl_about: RelativeLayout? = null
    private lateinit var topDialog: TopDialog
    private lateinit var rvDialog: RvAnimDialog
    private lateinit var tipDialog: MyDialog

    override fun onBindLayout() = R.layout.activity_system

    override fun initData() {
        if (mmkv.decodeBool("is_show_system_tip", true)) {
            if (this::tipDialog.isInitialized) tipDialog.show()
            else {
                tipDialog = MyDialog(this)
                tipDialog.run {
                    setIsShowSureOrCancel("确认", "不再提示")
                    setDialogText("部分设置需要重启APP才会生效")
                    setClickListener { v ->
                        when (v.id) {
                            R.id.tv_dialog_sure -> if (tipDialog.isShowing) tipDialog.dismiss()
                            R.id.tv_dialog_cancle -> {
                                if (tipDialog.isShowing) tipDialog.dismiss()
                                mmkv.encode("is_show_system_tip", false)
                            }
                        }
                    }
                    show()
                }
            }
        }
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun initView() {
        setTop("系统设置")
        sc_system_is_img = findViewById<SwitchCompat>(R.id.sc_system_is_img)
        sc_system_show_top = findViewById<SwitchCompat>(R.id.sc_system_show_top)
        sc_system_show_banner = findViewById<SwitchCompat>(R.id.sc_system_show_banner)
        sc_system_show_badge = findViewById<SwitchCompat>(R.id.sc_system_show_badge)
        rl_app_default_page = findViewById<RelativeLayout>(R.id.rl_app_default_page)
        rl_rv_anmi = findViewById<RelativeLayout>(R.id.rl_rv_anmi)
        rl_system_clear = findViewById<RelativeLayout>(R.id.rl_system_clear)
        rl_about = findViewById<RelativeLayout>(R.id.rl_about)
        sc_system_is_img?.run {
            isChecked = SettingUtil.getIsNoPhotoMode()
            setOnCheckedChangeListener { _, isChecked -> SettingUtil.setIsNoPhotoMode(isChecked) }
        }
        sc_system_show_top?.run {
            isChecked = SettingUtil.getIsShowTopArticle()
            setOnCheckedChangeListener { _, isChecked -> SettingUtil.setIsShowTopArticle(isChecked) }
        }
        sc_system_show_banner?.run {
            isChecked = SettingUtil.getIsShowBanner()
            setOnCheckedChangeListener { _, isChecked -> SettingUtil.setIsShowBanner(isChecked) }
        }
        sc_system_show_badge?.run {
            isChecked = SettingUtil.getIsShowBadge()
            setOnCheckedChangeListener { _, isChecked ->
                SettingUtil.setIsShowBadge(isChecked)
                if (mmkv.decodeBool(Constant.IS_LOGIN, false)) {
                    LiveEventBus.get("myBadge").post(isChecked)
                } else LiveEventBus.get("myBadge").post(false)
                if (!isChecked) LiveEventBus.get("homeBadge").post(0)
                else LiveEventBus.get("refresh_homeBadge").post(true)
            }
        }
        val tv_app_default_page = findViewById<TextView>(R.id.tv_app_default_page)
        rl_app_default_page?.run {
            tv_app_default_page.text = SettingUtil.getDefaultPage()
            setOnClickListener {
                if (this@SystemActivity::topDialog.isInitialized) topDialog.show()
                else {
                    topDialog = TopDialog(
                        this@SystemActivity,
                        mToolbar!!.height - StatusBarUtils.getStatusBarHeight(this@SystemActivity)
                    )
                    topDialog.show()
                }
            }
        }
        val tv_rv_anim = findViewById<TextView>(R.id.tv_rv_anim)
        rl_rv_anmi?.run {
            tv_rv_anim.text = SettingUtil.getListAnimal()
            setOnClickListener {
                if (this@SystemActivity::rvDialog.isInitialized) rvDialog.show()
                else {
                    rvDialog = RvAnimDialog(this@SystemActivity, mClickListener)
                    rvDialog.show()
                }
                setRvAnimColor()
            }
        }
        val tv_system_clear_num = findViewById<TextView>(R.id.tv_system_clear_num)
        rl_system_clear?.run {
            tv_system_clear_num.text = CacheDataUtil.getTotalCacheSize() ?: "0.0KB"
            setOnClickListener {
                if (CacheDataUtil.clearAllCache()) {
                    tv_system_clear_num.text = CacheDataUtil.getTotalCacheSize() ?: "0.0KB"
                }
                toast("清理成功")
            }
        }
        rl_about?.run {
            setOnClickListener {
                startActivity(Intent(this@SystemActivity, AboutActivity::class.java))
            }
        }
    }

    private val mClickListener = View.OnClickListener {
        when (it.id) {
            R.id.tv_rv_no -> {
                SettingUtil.setListAnimal(it.findViewById<TextView>(R.id.tv_rv_no).text.toString())
            }
            R.id.tv_rv_jx -> {
                SettingUtil.setListAnimal(it.findViewById<TextView>(R.id.tv_rv_jx).text.toString())
            }
            R.id.tv_rv_db -> {
                SettingUtil.setListAnimal(it.findViewById<TextView>(R.id.tv_rv_db).text.toString())
            }
            R.id.tv_rv_sf -> {
                SettingUtil.setListAnimal(it.findViewById<TextView>(R.id.tv_rv_sf).text.toString())
            }
            R.id.tv_rv_zc -> {
                SettingUtil.setListAnimal(it.findViewById<TextView>(R.id.tv_rv_zc).text.toString())
            }
            R.id.tv_rv_yc -> {
                SettingUtil.setListAnimal(it.findViewById<TextView>(R.id.tv_rv_yc).text.toString())
            }
        }
        LiveEventBus.get("rv_anim").post(SettingUtil.getListAnimal())
        findViewById<TextView>(R.id.tv_rv_anim).text = SettingUtil.getListAnimal()
        rvDialog.dismiss()
    }

    private fun setRvAnimColor() {
        rvDialog.tv_rv_no.setTextColor(resources.getColor(R.color.color_333))
        rvDialog.tv_rv_jx.setTextColor(resources.getColor(R.color.color_333))
        rvDialog.tv_rv_db.setTextColor(resources.getColor(R.color.color_333))
        rvDialog.tv_rv_sf.setTextColor(resources.getColor(R.color.color_333))
        rvDialog.tv_rv_zc.setTextColor(resources.getColor(R.color.color_333))
        rvDialog.tv_rv_yc.setTextColor(resources.getColor(R.color.color_333))
        when (SettingUtil.getListAnimal()) {
            "无" -> rvDialog.tv_rv_no.setTextColor(resources.getColor(R.color.colorPrimaryDark))
            "渐显" -> rvDialog.tv_rv_jx.setTextColor(resources.getColor(R.color.colorPrimaryDark))
            "缩放" -> rvDialog.tv_rv_sf.setTextColor(resources.getColor(R.color.colorPrimaryDark))
            "底部滑入" -> rvDialog.tv_rv_db.setTextColor(resources.getColor(R.color.colorPrimaryDark))
            "左侧滑入" -> rvDialog.tv_rv_zc.setTextColor(resources.getColor(R.color.colorPrimaryDark))
            "右侧滑入" -> rvDialog.tv_rv_yc.setTextColor(resources.getColor(R.color.colorPrimaryDark))
        }
    }
}